import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JPanel;


public class GameMenu extends ScreenManager{

	private static final long serialVersionUID = 1L;
	private String userName = "";
	private final JComboBox<String> levelComboBox;
	private static int levels;
	
	public GameMenu(String name){
		userName = name;
		JPanel gameMenu = new JPanel();
		//gameMenu.setBackground(Color.white);
		getContentPane().add(gameMenu);
		levels = levelOfUser();
		if(levels >= 15) levels = 15;
			String[] str = new String[levels];
		for(int i = 0; i < levels; i++)
			str[i] = "Level " + (i + 1);
		levelComboBox = new JComboBox<String>(str);
		levelComboBox.setBounds(155, 25, 75, 20);
		gameMenu.add(levelComboBox);
		
		ImageIcon startBg = new ImageIcon(getClass().getResource("bgs/startGame.jpg"));
		ImageIcon backBg = new ImageIcon(getClass().getResource("bgs/backBg.jpg"));
		ImageIcon loadBg = new ImageIcon(getClass().getResource("bgs/loadBg.jpg"));
		
		
		gameMenu.setLayout(null);
		JButton playButton = new JButton("Start Game", startBg);
		playButton.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	closeTab();
		    	//ComboBox
		    	int levelNo = levelComboBox.getSelectedIndex() + 1;
		    	GameEngine playMenu = new GameEngine(userName, levelNo);
		    	playMenu.setVisible(true);
		    }
		}); 
		JButton loadButton = new JButton("Load Game", loadBg);
		loadButton.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	closeTab();
		    	//ComboBox
		    	GameEngine playMenu = new GameEngine(userName);
		    	playMenu.setVisible(true);
		    }
		});
		JButton backButton = new JButton("Back Game", backBg);
		backButton.addActionListener( new ActionListener()
		{
		    public void actionPerformed(ActionEvent e)
		    {
		    	closeTab();
		    	MainMenu mainMenu = new MainMenu(userName);
		    	mainMenu.setVisible(true);
		    }
		});
		playButton.setBounds(145, 115, 95, 55);
		backButton.setBounds(145, 295, 95, 55);
		loadButton.setBounds(145, 205, 95, 55);
		gameMenu.add(playButton);
		gameMenu.add(backButton);
		gameMenu.add(loadButton);
		
		
		
	}
	private int levelOfUser(){
		DatabaseManager.openConnection();
		Statement stmt = null;
		int returnLevel = 0;
		try {
			String sql = "select levelId from userlevels inner join users on users.userId = userlevels.userId where users.userName = '"+ userName +"'";
			stmt = DatabaseManager.conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next())
				returnLevel = rs.getInt("levelId");
			rs.close();
		}catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		DatabaseManager.closeConnection();
		return returnLevel;
	}
	private void closeTab(){
    	this.dispatchEvent(new WindowEvent(this,WindowEvent.WINDOW_CLOSING));
	}
	public static int getLevels() {
		return levels;
	}
}
